﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class TabsDesigner : CellTypeDesigner<Tabs>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            var cellStyleViewModel = StyleHelper.CreateStyleViewModel(cellInfo, drawingHelper);
            return new TabHeaderDrawingObject(this.CellType, cellStyleViewModel, cellInfo, drawingHelper);
        }

        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            for (int i = 0; i < 3; i++)
            {
                this.CellType.tabs.Add(new Tab()
                {
                    value = (i + 1).ToString(),
                    title = Resources.Tabs_DefaultName + (i + 1).ToString()
                });
            }
            this.CellType.background = "#ffffff";
            this.CellType.color = "Accent 1";
        }
    }

    [Designer("Vant.CellTypes.TabsDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Tabs.png")]
    [OrderWeight((int)Utils.OrderWeight.Tabs)]
    public class Tabs : VantCellTypeBase, ISupportDefaultValue
    {
        [SRDisplayName(nameof(Resources.Tabs_EditClickCommand))]
        [SRCustomCommandObject(InitParamProperties = "itemIndex|itemValue|itemText", InitParamValues = nameof(Resources.Tabs_ClickCommand_InitParamValues))]
        public object ClickCommand { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_DefaultValue))]
        public object DefaultValue { get; set; }

        [SRDisplayName(nameof(Resources.UseBinding))]
        [BoolProperty]
        [MergableProperty(false)]
        public bool useBinding { get; set; } = false;

        [SRDisplayName(nameof(Resources.Tabs_tabs))]
        [SRObjectListProperty(ItemType = typeof(Tab), DefaultName = nameof(Resources.Tabs_DefaultName), IndentLevel = 1)]
        public List<INamedObject> tabs { get; set; } = new List<INamedObject>();

        [SRBindingDataSourceProperty(Columns = nameof(Resources.Tabs_BindingDataSourceProperty_Columns), IndentLevel = 1)]
        [SRDisplayName(nameof(Resources.BindingOptions))]
        public object bindingOptions { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_Type))]
        [SRComboProperty(ValueList = "line|card", DisplayList = nameof(Resources.Tabs_Type_DisplayList))]
        public string type { get; set; } = "line";

        [SRDisplayName(nameof(Resources.Tabs_Background))]
        [ColorProperty(SupportNoFill = true)]
        public string background { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_Color))]
        [ColorProperty]
        public string color { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_TitleActiveColor))]
        [ColorProperty(SupportNoFill = true)]
        public string titleActiveColor { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_TitleInactiveColor))]
        [ColorProperty(SupportNoFill = true)]
        public string titleInactiveColor { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_LineWidth))]
        [IntProperty]
        [DefaultValue(40)]
        public int lineWidth { get; set; } = 40;

        [SRDisplayName(nameof(Resources.Tabs_LineHeight))]
        [IntProperty]
        [DefaultValue(3)]
        public int lineHeight { get; set; } = 3;

        [SRDisplayName(nameof(Resources.Tabs_Ellipsis))]
        [BoolProperty]
        [DefaultValue(true)]
        public bool ellipsis { get; set; } = true;

        [SRDisplayName(nameof(Resources.Tabs_Shrink))]
        [BoolProperty]
        public bool shrink { get; set; } = false;
        public bool NeedFormatDefaultValue => true;

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.RunTimeMethod_SetBadge))]
        public void SetBadge(
            [SRItemDisplayName(nameof(Resources.RunTimeMethod_SetBadge_itemValue))]
            string itemValue,
            [SRItemDisplayName(nameof(Resources.RunTimeMethod_SetBadge_badgeValue))]
            string badgeValue)
        {

        }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(tabs))
            {
                return !useBinding;
            }
            if (propertyName == nameof(bindingOptions))
            {
                return useBinding;
            }
            if (propertyName == nameof(lineWidth) || propertyName == nameof(lineHeight))
            {
                return type == "line";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.TabHeader_HideItems_Display))]
        public void HideItems(
        [SRItemDisplayName(nameof(Resources.TabHeader_HideItemsParam_Items))]
            [SRDescription(nameof(Resources.TabHeader_HideItemsParam_ItemsDescription))]
            object items)
        {
        }


        public override string ToString()
        {
            return Resources.Tabs_DisplayName;
        }
    }
 
    public class Tab : ObjectPropertyBase, INamedObject
    {
        [SRDisplayName(nameof(Resources.Tabs_Tab_Value))]
        [Required]
        public string value { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_Tab_Title))]
        [Required]
        public string title { get; set; }

        [SRDisplayName(nameof(Resources.Tabs_Tab_Dot))]
        [SRComboProperty(ValueList = "true|false", DisplayList = nameof(Resources.Tabs_Dot_DisplayList))]
        public bool dot { get; set; } = false;

        [SRDisplayName(nameof(Resources.Tabs_Tab_Badge))]
        [FormulaProperty]
        public object badge { get; set; }

        string INamedObject.Name
        {
            get => title;
            set
            {
                title = value;
            }
        }
    }
}
